﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Components;

namespace WorkflowDesigner
{
    public partial class Toolbox : UserControl
    {
        public event OnDragToolItem DragToolItem;
        public WorkflowComponentBase DraggedObject
        {
            get; set;
        }

        public Toolbox()
        {
            InitializeComponent();
            this.MouseMove += new MouseEventHandler(Toolbox_MouseMove);
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.PopulateToolItems();
        }

        private void PopulateToolItems()
        {
            this.LayoutRoot.Children.Clear();
            Dictionary<string,List<WorkflowComponentBase>> componentsByCategories=new Dictionary<string, List<WorkflowComponentBase>>();
            foreach(string typeName in WorkflowEngine.ComponentTypes.Keys)
            {
                Type componentType = WorkflowEngine.ComponentTypes[typeName];
                WorkflowComponentBase component = (WorkflowComponentBase) Activator.CreateInstance(componentType);
                if(componentsByCategories.ContainsKey(component.ComponentCategory))
                {
                    List<WorkflowComponentBase> components = componentsByCategories[component.ComponentCategory];
                    components.Add(component);
                    componentsByCategories[component.ComponentCategory] = components;
                }
                else
                {
                    List<WorkflowComponentBase> components=new List<WorkflowComponentBase>();
                    components.Add(component);
                    componentsByCategories.Add(component.ComponentCategory,components);
                }
            }
            List<string> categories=new List<string>();
            categories.AddRange(componentsByCategories.Keys);
            categories.Sort();
            foreach (string category in categories)
            {
                Expander exCategory=new Expander();
                exCategory.Header = category;
                exCategory.BorderBrush = new SolidColorBrush(Colors.Gray);
                exCategory.BorderThickness = new Thickness(0, 0, 1.0, 1.0);
                exCategory.Background = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
                this.LayoutRoot.Children.Add(exCategory);
                StackPanel categoryStack = new StackPanel();
                categoryStack.Background = new SolidColorBrush(Colors.White);
                categoryStack.Orientation = Orientation.Vertical;
                exCategory.Content = categoryStack;
                categoryStack.MouseMove += new MouseEventHandler(categoryCanvas_MouseMove);
                // double x = 3, y = 3;
                List<WorkflowComponentBase> components = componentsByCategories[category];
                components.Sort();
                foreach(WorkflowComponentBase component in components)
                {
                    ToolItem toolItem=new ToolItem(component);
                    toolItem.SelectToolItem += new OnSelectToolItem(toolItem_SelectToolItem);
                    categoryStack.Children.Add(toolItem);
                    //toolItem.SetValue(Canvas.LeftProperty,x);
                    //toolItem.SetValue(Canvas.TopProperty,y);
                    //y += toolItem.IconSize.Height + 3;
                }
                exCategory.IsExpanded = true;
            }
        }

        void categoryCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.DraggedObject != null && this.DragToolItem != null)
                this.DragToolItem(this.DraggedObject);
        }

        void Toolbox_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.DraggedObject != null && this.DragToolItem != null)
                this.DragToolItem(this.DraggedObject);
        }

        void toolItem_SelectToolItem(WorkflowComponentBase userObj)
        {
            this.DraggedObject = userObj;
        }
    }
}
